package com.example.demo.controller;

import com.example.demo.entity.Licence;
import com.example.demo.entity.Response;
import com.example.demo.entity.Subject;
import com.example.demo.service.Impl.SubjectImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import static java.time.LocalDateTime.now;
import static java.util.Map.of;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.OK;

@RestController
@RequestMapping("/subject")
@RequiredArgsConstructor
public class SubjectController {

    private final SubjectImpl subjectImpl;
    @GetMapping("/findAllSubject")
    public ResponseEntity<Response> findAllSubject() {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("getAllSubject",subjectImpl.findAllSubject()))
                        .message("获取所有科目信息")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }
    @PostMapping("/addSubject")
    public ResponseEntity<Response> addSubject(@RequestBody Subject subject) {
        try {
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("addLicence",subjectImpl.addSubject(subject)))
                            .message("添加科目")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(null)
                            .message("添加科目失败")
                            .status(BAD_REQUEST)
                            .statusCode(BAD_REQUEST.value())
                            .build()
            );
        }
    }
    @DeleteMapping("/deleteSubject")
    public ResponseEntity<Response> deleteSubject(@RequestParam Long subjectId) {
        try {
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("deleteSubject",subjectImpl.deleteSubject(subjectId)))
                            .message("删除科目")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(null)
                            .message("删除科目失败")
                            .status(BAD_REQUEST)
                            .statusCode(BAD_REQUEST.value())
                            .build()
            );
        }
    }
    @PostMapping("/updateSubject")
    public ResponseEntity<Response> updateSubject(@RequestBody Subject subject) {
        try {
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("updateSubject",subjectImpl.updateSubject(subject)))
                            .message("更新科目")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(null)
                            .message("更新科目失败")
                            .status(BAD_REQUEST)
                            .statusCode(BAD_REQUEST.value())
                            .build()
            );
        }
    }

}
